<!-- 新增/编辑分类弹框 -->
<template>
  <el-dialog :visible.sync="dialogVisible" :title="detail && detail.goodsTypeId ? '编辑分类' : '新增分类'" width="30%"
    @open="open" @close="close">
    <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef" label-width="80px">
      <el-form-item label="序号" prop="sortNo">
        <el-input v-model="ruleForm.sortNo"></el-input>
      </el-form-item>
      <el-form-item label="分类名称" prop="goodsTypeName">
        <el-input v-model="ruleForm.goodsTypeName"></el-input>
      </el-form-item>
      <el-form-item v-if="exchangeType == 0" label="是否定向兑换" prop="isDirectedExchange" label-width="108px">
        <el-select v-model="ruleForm.isDirectedExchange" placeholder="请选择是否定向兑换" style="width: 100%;">
          <el-option label="是" :value="1"></el-option>
          <el-option label="否" :value="0"></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleFormRef')" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addGoodsExchangeType,
  editGoodsExchangeType
} from '@/api/goodsExchange';
export default {
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    exchangeType: {
      type: [String, Number],
      default: '',
    },
    detail: {
      type: Object,
      default: () => { },
    },
  },
  data() {
    return {
      ruleForm: {
        sortNo: '',
        goodsTypeName: '',
        isDirectedExchange: 0
      },
      loading: false
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit('update:show', val);
      },
    },
    rules: function () {
      const defaultRules = {
        rules: {
          goodsTypeName: [
            { required: true, message: '请输入分类名称', trigger: 'blur' },
          ],
          sortNo: [
            {
              validator: (rule, value, callback) => {
                if (value) {
                  if (!/^\d+$/.test(value)) {
                    callback(new Error('请输入数字'));
                  } else {
                    callback();
                  }
                } else {
                  callback();
                }
              }, trigger: 'blur'
            }
          ]
        },
      }
      return this.exchangeType == 0 ? 
        { ...defaultRules.rules, isDirectedExchange: [{ required: true, message: '请选择是否定向兑换', trigger: 'change' }] }
        : defaultRules.rules;
    }
  },
  methods: {
    open() {
      if (this.detail && this.detail.goodsTypeId) {
        this.ruleForm.goodsTypeName = this.detail.goodsTypeName;
        this.ruleForm.sortNo = this.detail.sortNo;
      } else {
        this.ruleForm.goodsTypeName = '';
        this.ruleForm.sortNo = '';
      }
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.detail && this.detail.goodsTypeId) {
            editGoodsExchangeType({
              goodsTypeId: this.detail.goodsTypeId,
              ...this.ruleForm,
              exchangeType: this.exchangeType,
              isDisplay: '1'
            }).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: res.message,
                  type: 'success',
                });
                this.dialogVisible = false;
                this.$emit('refresh');
              } else {
                this.$message({
                  message: res.message,
                  type: 'error',
                });
              }
            }).finally(() => {
              this.loading = false
            })
          } else {
            addGoodsExchangeType({
              ...this.ruleForm,
              exchangeType: this.exchangeType,
              isDisplay: '1'
            }).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: res.message,
                  type: 'success',
                });
                this.dialogVisible = false;
                this.$emit('refresh');
              } else {
                this.$message({
                  message: res.message,
                  type: 'error',
                });
              }
            }).finally(() => {
              this.loading = false
            })
          }
        }
      })
    },

    close() {
      this.$refs['ruleFormRef'].resetFields();
    }
  }
}
</script>